﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Data;
using Data.Common;
using Data.Enum;

namespace DataAccess.BikeManage.logical
{
    #region 系统用到的类

    /// <summary>
    /// 权限
    /// </summary>
    public enum Purview
    {
        Maintain0 = 0,
        Maintain1 = 1,
        Maintain2 = 2,
        Maintain3 = 3,

        /// <summary>
        /// 消费
        /// </summary>
        Consume = 4,

        /// <summary>
        /// 充值
        /// </summary>
        Recharge = 5,

        /// <summary>
        /// 只读
        /// </summary>
        ReadOnly = 6,

        /// <summary>
        /// 读写
        /// </summary>
        ReadWrite = 7
    }

    /// <summary>
    /// cpu卡中的用户信息类v1版本，交接第一次，废弃
    /// </summary>
    public class CpuAccountV1
    {
        /// <summary>
        /// cpu卡序列号
        /// </summary>
        public string CpuCardId { get; set; }

        /// <summary>
        /// cpu卡版本 ascii
        /// </summary>
        public string CpuCardVersion { get; set; }
        public byte[] baCardVer { get; set; }

        /// <summary>
        /// cpu卡编号 ascii
        /// </summary>
        public string CpuCardNo { get; set; }

        /// <summary>
        /// cpu卡类型 unicode
        /// </summary>
        public string CpuCardType { get; set; }

        /// <summary>
        /// cpu卡状态 unicode
        /// </summary>
        public string CpuCardStatus { get; set; }

        /// <summary>
        /// cpu卡开户日期 ascii
        /// </summary>
        public string CpuCardOpenDate { get; set; }

        /// <summary>
        /// cpu卡有效期天数 ascii
        /// </summary>
        public string CpuCardValideDays { get; set; }

        /// <summary>
        /// 用户名 unicode
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// 性别 unicode
        /// </summary>
        public string Sex { get; set; }

        /// <summary>
        /// 名族 unicode
        /// </summary>
        public string Nation { get; set; }

        /// <summary>
        /// 证件类型 unicode
        /// </summary>
        public string IdCardType { get; set; }

        /// <summary>
        /// 证件号码 ascii
        /// </summary>
        public string IdCardNo { get; set; }

        /// <summary>
        /// 发证单位 unicode
        /// </summary>
        public string IdCardGov { get; set; }

        /// <summary>
        /// 移动号码 ascii
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 电话号码 ascii
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 费率 ascii
        /// </summary>
        public string Fee { get; set; }
    }

    /// <summary>
    /// cpu卡用户信息
    /// </summary>
    public class CpuAccount
    {
        /// <summary>
        /// cpu卡序列号
        /// </summary>
        public string CpuCardId { get; set; }

        /// <summary>
        /// cpu卡版本 ascii
        /// </summary>
        public string CpuCardVersion { get; set; }

        /// <summary>
        /// cpu卡版本 ByteArray
        /// </summary>
        public byte[] baCardVer { get; set; }

        /// <summary>
        /// cpu卡编号 ascii
        /// </summary>
        public string CpuCardNo { get; set; }

        /// <summary>
        /// 用户名 unicode
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// 性别 unicode
        /// </summary>
        public string Sex { get; set; }

        /// <summary>
        /// 名族 unicode
        /// </summary>
        public string Nation { get; set; }

        /// <summary>
        /// 证件类型 unicode
        /// </summary>
        public string IdCardType { get; set; }

        /// <summary>
        /// 证件号码 ascii
        /// </summary>
        public string IdCardNo { get; set; }

        /// <summary>
        /// 家庭住址unicode
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 移动号码 ascii
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 电话号码 ascii
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// cpu卡类型 unicode
        /// </summary>
        public string CpuCardType { get; set; }

        /// <summary>
        /// cpu卡状态 unicode
        /// </summary>
        public string CpuCardStatus { get; set; }

        /// <summary>
        /// 费率 ascii
        /// </summary>
        public string Fee { get; set; }
    }

    /// <summary>
    /// 卡里面记录的交易记录
    /// </summary>
    public class DealRecord
    {
        /// <summary>
        /// 不是从交易记录直接查出来的
        /// </summary>
        public string CpuId { get; set; }

        /// <summary>
        /// 交易序列号
        /// </summary>
        public string DealId { get; set; }

        /// <summary>
        /// 透支金额
        /// </summary>
        public decimal CreditMoney { get; set; }

        /// <summary>
        /// 消费金额
        /// </summary>
        public decimal DealMoney { get; set; }

        /// <summary>
        /// 交易类型
        /// </summary>
        public byte DealType { get; set; }

        /// <summary>
        /// 终端机编号
        /// </summary>
        public string TerminalId { get; set; }

        /// <summary>
        /// 消费时间
        /// </summary>
        public string DealTime { get; set; }
    }

    /// <summary>
    /// 借车类
    /// </summary>
    public class BorrowBike
    {
        /// <summary>
        /// 开户日期20131010
        /// </summary>
        public string OpenAccountDate { get; set; }

        /// <summary>
        /// 截止日期20131010
        /// </summary>
        public string EndAccountDate { get; set; }

        /// <summary>
        /// 借车柱的psam卡id
        /// </summary>
        public string BorrowBikePsamid { get; set; }

        /// <summary>
        /// 用户卡id
        /// </summary>
        public string BorrowCpuid { get; set; }

        /// <summary>
        /// 车辆卡id
        /// </summary>
        public string BorrowBikeid { get; set; }

        /// <summary>
        /// 借车时间
        /// </summary>
        public string BorrowTime { get; set; }
    }

    /// <summary>
    /// cpu卡设备的信息
    /// </summary>
    public class CpuEquipmentInfo
    {
        /// <summary>
        /// cpu卡设备状态
        /// </summary>
        private bool _status = false;
        public bool Status { get { return _status; } set { _status = value; } }

        /// <summary>
        /// 设备id
        /// </summary>
        private int icdev = 0;
        public int Icdev { get { return icdev; } set { icdev = value; } }

        /// <summary>
        /// psam终端机编号
        /// </summary>
        private byte[] zdbh = new byte[6];
        public byte[] Zdbh { get { return zdbh; } set { zdbh = value; } }

        /// <summary>
        /// psam卡密钥用途
        /// </summary>
        private byte[] keyfun = new byte[1];
        public byte[] Keyfun { get { return keyfun; } set { keyfun = value; } }
    }

    #endregion

    /// <summary>
    /// cpu卡操作逻辑类
    /// </summary>
    public class Zq58Logical
    {
        #region 引用函数

        /// <summary>
        /// 连接发卡器
        /// </summary>
        /// <returns>成功则返回状态标识符>0</returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_Connect")]
        public static extern int ZQ58_Connect();

        /// <summary>
        /// 发卡器蜂鸣器鸣叫
        /// </summary>
        /// <param name="icdev">数据为打开端口所返回的端口标识符</param>
        /// <param name="BeepTime">鸣叫时间，单位为10ms</param>
        /// <returns>成功则返回状态标识符0，失败返回其他值，见错误代码表</returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_Beep")]
        public static extern int ZQ58_Beep(int icdev, byte BeepTime);

        /// <summary>
        /// PSAM卡初始化
        /// </summary>
        /// <param name="icdev">数据为打开端口所返回的端口标识符</param>
        /// <param name="card_simid">PSAM卡所在的SIM卡漕位置;        	1字节</param>
        /// <param name="ZDBH">PSAM卡的终端机编号;       		6字节</param>
        /// <param name="KeyFun">PSAM卡的密钥用途;     		1字节</param>
        /// <returns>成功则返回状态标识符0，失败返回其他值，见错误代码表</returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_PsamInit")]
        public static extern int ZQ58_PsamInit(int icdev, byte card_simid, ref byte ZDBH, ref byte KeyFun);

        /// <summary>
        /// CPU卡初始化
        /// </summary>
        /// <param name="icdev">数据为打开端口所返回的端口标识符</param>
        /// <param name="card_simid">PSAM卡所在的SIM卡漕位置;        	1字节</param>
        /// <param name="CpuUid">CPU卡的序列号;       		4字节</param>
        /// <param name="LFlen">CPU卡的0014长文件首字节长度;	1字节</param>
        /// <returns>成功则返回状态标识符0，失败返回其他值，见错误代码表</returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_CpuInit")]
        public static extern int ZQ58_CpuInit(int icdev, byte card_simid, ref byte CpuUid, ref byte LFlen);

        /// <summary>
        /// 只读双向认证
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_ReadAuth")]
        public static extern int ZQ58_ReadAuth(int icdev, byte card_simid);

        /// <summary>
        /// 读写双向认证
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_WriteAuth")]
        public static extern int ZQ58_WriteAuth(int icdev, byte card_simid);

        /// <summary>
        /// 读0009文件
        /// </summary>
        /// <param name="icdev">数据为打开端口所返回的端口标识符</param>
        /// <param name="card_simid">PSAM卡所在的SIM卡漕位置;        	1字节</param>
        /// <param name="FileAdd">文件起始地址;       		1字节</param>
        /// <param name="FileLen">文件长度;	                           1字节</param>
        /// <param name="FileData">文件数据;                           最大192字节</param>
        /// <returns>成功则返回状态标识符0，失败返回其他值，见错误代码表</returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_Read0009")]
        public static extern int ZQ58_Read0009(int icdev, byte card_simid, byte FileAdd, byte FileLen, ref byte FileData);

        /// <summary>
        /// 写0009文件
        /// </summary>
        /// <param name="icdev">数据为打开端口所返回的端口标识符;</param>
        /// <param name="card_simid">PSAM卡所在的SIM卡漕位置;        	1字节</param>
        /// <param name="FileAdd">文件起始地址;       		1字节</param>
        /// <param name="FileLen">文件长度;	                           1字节</param>
        /// <param name="FileData">文件数据;                           最大192字节</param>
        /// <returns>成功则返回状态标识符0，失败返回其他值，见错误代码表</returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_Write0009")]
        public static extern int ZQ58_Write0009(int icdev, byte card_simid, byte FileAdd, byte FileLen, ref byte FileData);

        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_PsamPin")]
        public static extern int ZQ58_PsamPin(int icdev, byte card_simid, ref byte PinValue);

        /// <summary>
        /// 钱包充值
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="Amount"></param>
        /// <param name="DateTime"></param>
        /// <param name="rData"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_EPCZ")]
        public static extern int ZQ58_EPCZ(int icdev, byte card_simid, ref byte Amount, ref byte DateTime, ref byte rData);

        /// <summary>
        /// 读钱包余额
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="Amount"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_EPBalance")]
        public static extern int ZQ58_EPBalance(int icdev, byte card_simid, ref byte Amount);

        /// <summary>
        /// 获取sw错误信息
        /// </summary>
        /// <param name="cardSW"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_GetSW")]
        public static extern int ZQ58_GetSW(ref byte cardSW);

        /// <summary>
        /// 补刷卡
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="rLen"></param>
        /// <param name="rData"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_BSCard")]
        public static extern int ZQ58_BSCard(int icdev, byte card_simid, ref byte rLen, ref byte rData);

        /// <summary>
        /// 读取交易记录
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="RecordNo"></param>
        /// <param name="rData"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_ReadJYRecord")]
        public static extern int ZQ58_ReadJYRecord(int icdev, byte card_simid, byte RecordNo, ref byte rData);

        /// <summary>
        /// 用作用户卡借车
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="FileAdd"></param>
        /// <param name="FileLen"></param>
        /// <param name="FileData"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_Read0010")]
        public static extern int ZQ58_Read0010(int icdev, byte card_simid, byte FileAdd, byte FileLen, ref byte FileData);

        /// <summary>
        /// 用户卡写借车
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="FileAdd"></param>
        /// <param name="FileLen"></param>
        /// <param name="FileData"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_Write0010")]
        public static extern int ZQ58_Write0010(int icdev, byte card_simid, byte FileAdd, byte FileLen, ref byte FileData);

        /// <summary>
        /// 消费
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="card_simid"></param>
        /// <param name="Amount"></param>
        /// <param name="DateTime"></param>
        /// <param name="rData"></param>
        /// <returns></returns>
        [DllImport("ZQ58DTK.dll", EntryPoint = "ZQ58_EPXF")]
        public static extern int ZQ58_EPXF(int icdev, byte card_simid, ref byte Amount, ref byte DateTime, ref byte rData);


        #endregion

        #region 通用方法

        /// <summary>
        /// 蜂鸣默认时间
        /// </summary>
        private byte _beepTime = 5;

        /// <summary>
        /// psam卡卡槽位置
        /// </summary>
        private byte _psamId = 1;

        /// <summary>
        /// psam密码
        /// </summary>
        private byte[] _psamPassword;

        /// <summary>
        /// cpu设备信息
        /// </summary>
        private static CpuEquipmentInfo _cpuEquipmentInfo = new CpuEquipmentInfo();

        /// <summary>
        /// 通过配置文件获取psam卡和设备的相关信息
        /// </summary>
        public Zq58Logical()
        {
            try
            {
                _beepTime = Convert.ToByte(ConfigurationManager.AppSettings["beepTime"]);
                _psamId = Convert.ToByte(ConfigurationManager.AppSettings["psamId"]);
                char[] str = ConfigurationManager.AppSettings["psamPassword"].ToCharArray();
                if (str.Count() > 0)
                {
                    _psamPassword = new byte[str.Count()];
                    for (int i = 0; i < str.Count(); i++)
                    {
                        _psamPassword[i] = Convert.ToByte(str[i].ToString());
                    }
                }

                #region account格式值初始化 交接废除
                ////格式值初始化
                //cpuCardVersion_begin = 0;
                //cpuCardId_begin = (byte)(cpuCardVersion_begin + cpuCardVersionLen);
                //displayName_begin = (byte)(cpuCardId_begin + cpuCardIdLen);
                //sex_begin = (byte)(displayName_begin + displayNameLen);
                //nation_begin = (byte)(sex_begin + sexLen);
                //idCardType_begin = (byte)(nation_begin + nationLen);
                //idCardNo_begin = (byte)(idCardType_begin + idCardTypeLen);
                //idCardGov_begin = (byte)(idCardNo_begin + idCardNoLen);
                //phone_begin = (byte)(idCardGov_begin + idCardGovLen);
                //mobile_begin = (byte)(phone_begin + phoneLen);
                //cpuCardType_begin = (byte)(mobile_begin + mobileLen);
                //cpuCardStatus_begin = (byte)(cpuCardType_begin + cpuCardTypeLen);
                //cpuCardOpenDate_begin = (byte)(cpuCardStatus_begin + cpuCardStatusLen);
                //cpuCardValideDays_begin = (byte)(cpuCardOpenDate_begin + cpuCardOpenDateLen);
                //fee_begin = (byte)(cpuCardValideDays_begin + cpuCardValideDaysLen);
                #endregion

                #region account格式值初始化
                //格式值初始化
                cpuCardVersion_begin = 0;
                cpuCardNo_begin = (byte)(cpuCardVersion_begin + cpuCardVersionLen);
                displayName_begin = (byte)(cpuCardNo_begin + cpuCardNoLen);
                sex_begin = (byte)(displayName_begin + displayNameLen);
                nation_begin = (byte)(sex_begin + sexLen);
                idCardType_begin = (byte)(nation_begin + nationLen);
                idCardNo_begin = (byte)(idCardType_begin + idCardTypeLen);
                addr_begin = (byte)(idCardNo_begin + idCardNoLen);
                mobile_begin = (byte)(addr_begin + addrLen);
                phone_begin = (byte)(mobile_begin + mobileLen);
                cpuCardType_begin = (byte)(phone_begin + phoneLen);
                cpuCardStatus_begin = (byte)(cpuCardType_begin + cpuCardTypeLen);
                fee_begin = (byte)(cpuCardStatus_begin + cpuCardStatusLen);
                #endregion

                #region finance格式值初始化

                dealId_begin = 0;
                creditMoney_begin = (byte)(dealId_begin + dealIdLen);
                dealMoney_begin = (byte)(creditMoney_begin + creditMoneyLen);
                dealType_begin = (byte)(dealMoney_begin + dealMoneyLen);
                terminalId_begin = (byte)(dealType_begin + dealTypeLen);
                dealTime_begin = (byte)(terminalId_begin + terminalIdLen);

                #endregion

                #region bike格式初始化

                openAccountDate_begin = 0;
                endAccountDate_begin = (byte)(openAccountDate_begin + openAccountDateLen);
                borrowBikepsamid_begin = (byte)(endAccountDate_begin + endAccountLen);
                borrowCpuid_begin = (byte)(borrowBikepsamid_begin + borrowBikepsamidLen);
                borrowBikeid_begin = (byte)(borrowCpuid_begin + borrowCpuidLen);
                borrowTime_begin = (byte)(borrowBikeid_begin + borrowBikeidLen);

                #endregion
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 根据错误代码返回提示字符
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetZ58ErrorInfo(int type)
        {
            string result = string.Empty;
            switch (type)
            {
                case 1:
                    result = "SIM卡漕内无SAM卡或卡错误";
                    break;
                case 2:
                    result = "SAM卡访问失败";
                    break;
                case 3:
                    result = "SAM卡应答错误";
                    break;
                case 5:
                    result = "SAM卡类型错误";
                    break;
                case 11:
                    result = "非接触用户卡寻卡失败";
                    break;
                case 12:
                    result = "非接触用户卡访问失败";
                    break;
                case 13:
                    result = "非接触用户卡应答错误";
                    break;
                case 15:
                    result = "非接触用户卡类型错误";
                    break;
                case 18:
                    result = "用户卡和PSAM卡系统不一致";
                    break;
                case 21:
                    result = "MAC不一致";
                    break;
                case 22:
                    result = "TAC不一致";
                    break;
                case 23:
                    result = "内部认证不一致";
                    break;
                case 31:
                    result = "消费失败，需补刷卡";
                    break;
                case 32:
                    result = "充值失败，需补刷卡";
                    break;
                case 33:
                    result = "不需要补刷卡";
                    break;
                case 40:
                    result = "读卡器未联机";
                    break;
                case 41:
                    result = "数据长度错误";
                    break;
                default:
                    result = "未知错误";
                    break;
            }
            return result;
        }

        /// <summary>
        /// 获取sw的错误信息
        /// </summary>
        /// <param name="sw1"></param>
        /// <param name="sw2"></param>
        /// <returns></returns>
        public string GetZ58SWErrorInfo(byte sw1, byte sw2)
        {
            string result = string.Empty;
            if (sw1 == 0x62 && sw2 == 0x81)
            {
                result = "回送的数据可能错误";
            }
            else if (sw1 == 0x62 && sw2 == 0x83)
            {
                result = "选择文件无效，文件或密钥校验错误";
            }
            else if (sw1 == 0x63 && (sw2 >= 0xC1) || (sw2 <= 0xCF))
            {
                result = "错误，，请检查后重试"; //还可以再尝试" + (sw2 - 0xC1) + "次";
            }
            else if (sw1 == 0x64 && sw2 == 0x00)
            {
                result = "状态标志未改变";
            }
            else if (sw1 == 0x65 && sw2 == 0x81)
            {
                result = "写 EEPROM 不成功";
            }
            else if (sw1 == 0x67 && sw2 == 0x00)
            {
                result = "错误的长度";
            }
            else if (sw1 == 0x69 && sw2 == 0x00)
            {
                result = "CLA 与线路保护要求不匹配";
            }
            else if (sw1 == 0x69 && sw2 == 0x01)
            {
                result = "无效的状态";
            }
            else if (sw1 == 0x69 && sw2 == 0x81)
            {
                result = "命令与文件结构不相容";
            }
            else if (sw1 == 0x69 && sw2 == 0x82)
            {
                result = "不满足安全状态";
            }
            else if (sw1 == 0x69 && sw2 == 0x83)
            {
                result = "密钥被锁死";
            }
            else if (sw1 == 0x69 && sw2 == 0x85)
            {
                result = "使用条件不满足";
            }
            else if (sw1 == 0x69 && sw2 == 0x87)
            {
                result = "无安全报文";
            }
            else if (sw1 == 0x69 && sw2 == 0x88)
            {
                result = "安全报文数据项不正确";
            }
            else if (sw1 == 0x6A && sw2 == 0x80)
            {
                result = "数据域参数错误";
            }
            else if (sw1 == 0x6A && sw2 == 0x81)
            {
                result = "不支持或卡中无 MF 或卡片已锁";
            }
            else if (sw1 == 0x6A && sw2 == 0x82)
            {
                result = "文件未找到";
            }
            else if (sw1 == 0x6A && sw2 == 0x83)
            {
                result = "记录未找到";
            }
            else if (sw1 == 0x6A && sw2 == 0x84)
            {
                result = "文件无足够空间";
            }
            else if (sw1 == 0x6A && sw2 == 0x86)
            {
                result = "参数 P1 P2 错误";
            }
            else if (sw1 == 0x6A && sw2 == 0x88)
            {
                result = "密钥未找到";
            }
            else if (sw1 == 0x6B && sw2 == 0x00)
            {
                result = "在达到 Le/Lc 字节之前文件结束，偏移量错误 ";
            }
            else if (sw1 == 0x6C)
            {
                result = "Le 错误";
            }
            else if (sw1 == 0x6E && sw2 == 0x00)
            {
                result = "无效的 CLA ";
            }
            else if (sw1 == 0x6F && sw2 == 0x00)
            {
                result = "数据无效";
            }
            else if (sw1 == 0x93 && sw2 == 0x02)
            {
                result = "MAC错误";
            }
            else if (sw1 == 0x93 && sw2 == 0x03)
            {
                result = "应用已被锁定";
            }
            else if (sw1 == 0x94 && sw2 == 0x01)
            {
                result = "金额不足";
            }
            else if (sw1 == 0x94 && sw2 == 0x03)
            {
                result = "密钥未找到";
            }
            else if (sw1 == 0x94 && sw2 == 0x06)
            {
                result = "所需的 MAC不可用";
            }
            return result;
        }



        /// <summary>
        /// 把字符串转换成byte，不足lenth的不管，超过的省略
        /// </summary>
        /// <param name="sr"></param>
        /// <param name="lenth">要转多少位</param>
        /// <returns></returns>
        public byte[] GetUnicodeBytes(string sr, int lenth)
        {
            byte[] result = new byte[lenth];
            try
            {
                byte[] dn = Encoding.GetEncoding("gb2312").GetBytes(sr);
                if (dn.Count() < lenth)
                {
                    for (int i = 0; i < dn.Count(); i++)
                    {
                        result[i] = dn[i];
                    }
                }
                else
                {
                    for (int i = 0; i < lenth; i++)
                    {
                        result[i] = dn[i];
                    }
                }
            }
            catch (Exception e)
            {
                result = new byte[lenth];
            }
            return result;
        }

        /// <summary>
        /// 把字符串转换成byte，不足lenth的不管，超过的省略
        /// </summary>
        /// <param name="sr"></param>
        /// <param name="lenth"></param>
        /// <returns></returns>
        public byte[] GetAsciiBytes(string sr, int lenth)
        {
            byte[] result = new byte[lenth];
            try
            {
                byte[] dn = Encoding.ASCII.GetBytes(sr);
                if (dn.Count() < lenth)
                {
                    for (int i = 0; i < dn.Count(); i++)
                    {
                        result[i] = dn[i];
                    }
                }
                else
                {
                    for (int i = 0; i < lenth; i++)
                    {
                        result[i] = dn[i];
                    }
                }
            }
            catch (Exception e)
            {
                result = new byte[lenth];
            }
            return result;
        }

        /// <summary>
        /// 填充字节，将sourcebytes的字符填充到targetbytes的指定位置
        /// </summary>
        /// <param name="targetBytes">从beginindex位置开始</param>
        /// <param name="sourceBytes">从0位置开始</param>
        /// <param name="beginIndex">targetbytes的指定起始位置</param>
        /// <param name="length">targetbytes的填充长度</param>
        public void FillBytes(byte[] targetBytes, byte[] sourceBytes, int beginIndex, int length)
        {
            try
            {
                for (int i = beginIndex; i < (beginIndex + length); i++)
                {
                    targetBytes[i] = sourceBytes[i - beginIndex];
                }
            }
            catch (Exception e)
            {

            }
        }

        /// <summary>
        /// 从sourcebytes拿出指定位置的字符
        /// </summary>
        /// <param name="sourceBytes"></param>
        /// <param name="beginIndex">sourcebytes的起始位置</param>
        /// <param name="length">要拿出的长度</param>
        /// <returns></returns>
        public byte[] ReFillBytes(byte[] sourceBytes, int beginIndex, int length)
        {
            byte[] result = new byte[length];
            try
            {
                if (sourceBytes.Count() >= (beginIndex + length))
                {
                    for (int i = beginIndex; i < (beginIndex + length); i++)
                    {
                        result[i - beginIndex] = sourceBytes[i];
                    }
                }
            }
            catch (Exception e)
            {
                result = new byte[length];
            }
            return result;
        }

        /// <summary>
        /// 将时间20131111101010转为7位byte型，年2位，月。。。分别一位共七位
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public byte[] DateTimeToBytes(string date)
        {
            byte[] result = new byte[7];
            if (!string.IsNullOrEmpty(date))
            {
                if (date.Length > 14)
                {
                    date = date.Substring(0, 14);
                }
                else
                {
                    date = date.PadRight(14, '0');
                }
                try
                {
                    string year = Convert.ToString(Convert.ToInt32(date.Substring(0, 4)), 16).PadLeft(4, '0');
                    byte year1 = Convert.ToByte(year.Substring(0, 2), 16);
                    byte year2 = Convert.ToByte(year.Substring(2, 2), 16);
                    byte month = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(4, 2)), 16), 16);
                    byte day = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(6, 2)), 16), 16);
                    byte hour = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(8, 2)), 16), 16);
                    byte min = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(10, 2)), 16), 16);
                    byte second = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(12, 2)), 16), 16);
                    //result[0] = year1;
                    //result[1] = year2;
                    //时间更改时间高低位
                    result[1] = year1;
                    result[0] = year2;
                    result[2] = month;
                    result[3] = day;
                    result[4] = hour;
                    result[5] = min;
                    result[6] = second;
                }
                catch (Exception e)
                {
                    result = new byte[7];
                }
            }
            return result;
        }

        /// <summary>
        /// 时间的bytes转换为string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public string BytesToDateTime(byte[] bytes)
        {
            string result = string.Empty;
            if (bytes != null && bytes.Length == 7)
            {
                try
                {
                    //string year = Convert.ToString(bytes[0], 16).PadLeft(2, '0') + Convert.ToString(bytes[1], 16).PadLeft(2, '0');
                    //更改时间高低位
                    string year = Convert.ToString(bytes[1], 16).PadLeft(2, '0') + Convert.ToString(bytes[0], 16).PadLeft(2, '0');
                    result = string.Format("{0}{1}{2}{3}{4}{5}", Convert.ToInt32(year, 16).ToString().PadLeft(4, '0'), Convert.ToInt16(Convert.ToString(bytes[2], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'), Convert.ToInt16(Convert.ToString(bytes[3], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'), Convert.ToInt16(Convert.ToString(bytes[4], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'), Convert.ToInt16(Convert.ToString(bytes[5], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'), Convert.ToInt16(Convert.ToString(bytes[6], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'));
                }
                catch (Exception)
                {
                    result = string.Empty;
                }
            }
            return result;
        }

        /// <summary>
        /// 20131010年月日
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public byte[] DateToBytes(string date)
        {
            byte[] result = new byte[4];
            if (!string.IsNullOrEmpty(date))
            {
                if (date.Length > 8)
                {
                    date = date.Substring(0, 8);
                }
                else
                {
                    date = date.PadRight(8, '0');
                }
                try
                {
                    string year = Convert.ToString(Convert.ToInt32(date.Substring(0, 4)), 16).PadLeft(4, '0');
                    byte year1 = Convert.ToByte(year.Substring(0, 2), 16);
                    byte year2 = Convert.ToByte(year.Substring(2, 2), 16);
                    byte month = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(4, 2)), 16), 16);
                    byte day = Convert.ToByte(Convert.ToString(Convert.ToInt32(date.Substring(6, 2)), 16), 16);
                    //result[0] = year1;
                    //result[1] = year2;
                    //更改时间高低位
                    result[1] = year1;
                    result[0] = year2;
                    result[2] = month;
                    result[3] = day;
                }
                catch (Exception e)
                {
                    result = new byte[4];
                }
            }
            return result;
        }

        /// <summary>
        /// 转成20131010年月日
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public string BytesToDate(byte[] bytes)
        {
            string result = string.Empty;
            if (bytes != null && bytes.Length == 4)
            {
                try
                {
                    //string year = Convert.ToString(bytes[0], 16).PadLeft(2, '0') + Convert.ToString(bytes[1], 16).PadLeft(2, '0');
                    //更改时间高低位
                    string year = Convert.ToString(bytes[1], 16).PadLeft(2, '0') + Convert.ToString(bytes[0], 16).PadLeft(2, '0');
                    result = string.Format("{0}{1}{2}", Convert.ToInt32(year, 16).ToString().PadLeft(4, '0'), Convert.ToInt16(Convert.ToString(bytes[2], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'), Convert.ToInt16(Convert.ToString(bytes[3], 16).ToString().PadLeft(2, '0'), 16).ToString().PadLeft(2, '0'));
                }
                catch (Exception)
                {
                    result = string.Empty;
                }
            }
            return result;
        }

        /// <summary>
        /// 钱转为bytes
        /// </summary>
        /// <param name="money"></param>
        /// <param name="bytesLen">转为钱的字节数</param>
        /// <returns></returns>
        public byte[] MoneyToBytes(Int32 money, int bytesLen)
        {
            byte[] result = new byte[bytesLen];
            if (money > 0)
            {
                try
                {
                    string ms = Convert.ToString(money, 16).PadLeft(bytesLen * 2, '0');
                    for (int i = 0; i < bytesLen; i++)
                    {
                        result[i] = Convert.ToByte(ms.Substring(i * 2, 2), 16); ;
                    }
                    //result[0] = Convert.ToByte(ms.Substring(0, 2), 16); ;
                    //result[1] = Convert.ToByte(ms.Substring(2, 2), 16); ;
                    //result[2] = Convert.ToByte(ms.Substring(4, 2), 16); ;
                    //result[3] = Convert.ToByte(ms.Substring(6, 2), 16); ;
                }
                catch (Exception)
                {
                    result = new byte[4];
                }
            }
            return result;
        }

        /// <summary>
        /// bytes转为钱
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public Int32 BytesToMoney(byte[] bytes)
        {
            Int32 result = 0;
            if (bytes != null)
            {
                try
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        sb.Append(Convert.ToString(bytes[i], 16).PadLeft(2, '0'));
                    }
                    string ms = sb.ToString();//string.Format("{0}{1}{2}{3}", Convert.ToString(bytes[0], 16).PadLeft(2, '0'), Convert.ToString(bytes[1], 16).PadLeft(2, '0'), Convert.ToString(bytes[2], 16).PadLeft(2, '0'), Convert.ToString(bytes[3], 16).PadLeft(2, '0'));
                    if (!string.IsNullOrEmpty(ms))
                    {
                        result = Convert.ToInt32(ms, 16);
                    }
                }
                catch (Exception)
                {
                    result = 0;
                }
            }
            return result;
        }

        /// <summary>
        /// 将钱从cpu卡中读取时缩小100倍，变成有小数点的数，由分转到元
        /// </summary>
        /// <param name="money"></param>
        /// <param name="rate">倍率，默认100</param>
        /// <returns></returns>
        public Decimal GetOutMoney(int money, int rate)
        {
            Decimal result = 0;
            try
            {
                result = money / 100.00M;
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 将钱从外面获取了后存入cpu卡时，放大100被，变成无小数点的数，由元转分
        /// </summary>
        /// <param name="money"></param>
        /// <param name="rate">倍率，默认100倍</param>
        /// <returns></returns>
        public int PutInMoney(Decimal money, int rate)
        {
            int result = 0;
            try
            {
                result = Convert.ToInt32(money * 100);
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 蜂鸣
        /// </summary>
        public void Beep()
        {
            try
            {
                if (!_cpuEquipmentInfo.Status)
                {
                    CheckCpuEquipment();
                }
                ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
            }
            catch (Exception)
            {

            }
        }

        #endregion

        #region 检查

        /// <summary>
        /// 检查用户卡连接，返回用户卡id
        /// </summary>
        /// <returns></returns>
        public ResultInfo<string> CheckCpuEquipment()
        {
            ResultInfo<string> result = new ResultInfo<string>();
            //连接
            int icdev = ZQ58_Connect();
            if (icdev > 0)
            {
                //蜂鸣提示连接上了
                //ZQ58_Beep(icdev, _beepTime);
                //初始化psam卡和cpu卡
                int status = -1;
                byte[] zdbh = new byte[6];//psam终端机编号
                byte[] keyfun = new byte[1];//psam卡密钥用途
                status = ZQ58_PsamInit(icdev, _psamId, ref zdbh[0], ref keyfun[0]);
                //ZQ58_Beep(icdev, _beepTime);
                if (status == 0)
                {
                    byte[] cpuid = new byte[6];//cpu卡序列号
                    byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                    status = ZQ58_CpuInit(icdev, _psamId, ref cpuid[0], ref lfen[0]);
                    //ZQ58_Beep(icdev, _beepTime);
                    if (status == 0)
                    {
                        //获取读的权限
                        status = ZQ58_ReadAuth(icdev, _psamId);
                        ZQ58_Beep(icdev, _beepTime);
                        if (status == 0)
                        {
                            result.ResultNumber = ResultEnum.Success;
                            result.Obj = string.Format("{0}{1}{2}{3}", Convert.ToString(cpuid[0], 16).PadLeft(2, '0'), Convert.ToString(cpuid[1], 16).PadLeft(2, '0'), Convert.ToString(cpuid[2], 16).PadLeft(2, '0'), Convert.ToString(cpuid[3], 16).PadLeft(2, '0'));//System.Text.Encoding.ASCII.GetString(cpuid);//把cpuid放进去，cpuid是不存的;
                            result.Obj = string.IsNullOrEmpty(result.Obj) ? "" : result.Obj.ToUpper();

                            _cpuEquipmentInfo.Icdev = icdev;
                            _cpuEquipmentInfo.Keyfun = keyfun;
                            _cpuEquipmentInfo.Zdbh = zdbh;
                            _cpuEquipmentInfo.Status = true;
                        }
                        else
                        {
                            _cpuEquipmentInfo.Status = false;

                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        _cpuEquipmentInfo.Status = false;

                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    _cpuEquipmentInfo.Status = false;

                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                _cpuEquipmentInfo.Status = false;

                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = "连接失败！\n请查看用户卡读卡设备是否连接正常" + "\\" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        /// <summary>
        /// 检查设备连接，返回设备psamid
        /// </summary>
        /// <returns></returns>
        public ResultInfo<string> CheckEquipment()
        {
            ResultInfo<string> result = new ResultInfo<string>();
            //连接
            int icdev = ZQ58_Connect();
            if (icdev > 0)
            {
                //蜂鸣提示连接上了
                //ZQ58_Beep(icdev, _beepTime);
                //初始化psam卡和cpu卡
                int status = -1;
                byte[] zdbh = new byte[6];//psam终端机编号
                byte[] keyfun = new byte[1];//psam卡密钥用途
                status = ZQ58_PsamInit(icdev, _psamId, ref zdbh[0], ref keyfun[0]);
                ZQ58_Beep(icdev, _beepTime);
                if (status == 0)
                {
                    //_cpuEquipmentInfo.Cpuid = cpuid;
                    _cpuEquipmentInfo.Icdev = icdev;
                    _cpuEquipmentInfo.Keyfun = keyfun;
                    //_cpuEquipmentInfo.Lfen = lfen;
                    _cpuEquipmentInfo.Zdbh = zdbh;
                    _cpuEquipmentInfo.Status = true;

                    result.ResultNumber = ResultEnum.Success;
                    result.Obj = string.Format("{0}{1}{2}{3}{4}{5}", Convert.ToString(zdbh[0], 16).PadLeft(2, '0'), Convert.ToString(zdbh[1], 16).PadLeft(2, '0'), Convert.ToString(zdbh[2], 16).PadLeft(2, '0'), Convert.ToString(zdbh[3], 16).PadLeft(2, '0'), Convert.ToString(zdbh[4], 16).PadLeft(2, '0'), Convert.ToString(zdbh[5], 16).PadLeft(2, '0'));//System.Text.Encoding.ASCII.GetString(cpuid);//把cpuid放进去，cpuid是不存的;
                    result.Obj = string.IsNullOrEmpty(result.Obj) ? "" : result.Obj.ToUpper();
                }
                else
                {
                    _cpuEquipmentInfo.Status = false;

                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                _cpuEquipmentInfo.Status = false;

                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = "连接失败，请查看用户卡读卡设备是否连接正常" + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }
        #endregion

        #region account

        #region 存入cpu卡的信息所占的地址位数 v1版本的，交接的时候废除

        ////unicode
        //private byte displayName_begin;
        //private byte displayNameLen = 18;

        ////unicode
        //private byte sex_begin;
        //private byte sexLen = 2;

        ////unicode
        //private byte nation_begin;
        //private byte nationLen = 10;

        ////unicode
        //private byte idCardType_begin;
        //private byte idCardTypeLen = 10;

        ////ascii
        //private byte idCardNo_begin;
        //private byte idCardNoLen = 20;

        ////unicode
        //private byte idCardGov_begin;
        //private byte idCardGovLen = 30;

        ////unicode
        ////private byte addr_begin;
        ////private byte addrLen = 70;

        ////ascii
        //private byte phone_begin;
        //private byte phoneLen = 12;

        ////ascii
        //private byte mobile_begin;
        //private byte mobileLen = 18;

        ////ascii
        //private byte cpuCardId_begin;
        //private byte cpuCardIdLen = 20;

        ////unicode
        //private byte cpuCardType_begin;
        //private byte cpuCardTypeLen = 10;

        ////ascii
        //private byte cpuCardVersion_begin;
        //private byte cpuCardVersionLen = 5;

        ////unicode
        //private byte cpuCardStatus_begin;
        //private byte cpuCardStatusLen = 10;

        ////ascii 20131110
        //private byte cpuCardOpenDate_begin;
        //private byte cpuCardOpenDateLen = 8;

        ////ascii 有效天数比如10天
        //private byte cpuCardValideDays_begin;
        //private byte cpuCardValideDaysLen = 7;

        ////ascii 1.2
        //private byte fee_begin;
        //private byte feeLen = 5;


        #endregion

        #region 存入cpu卡的用户信息

        //unicode
        private byte displayName_begin;
        private byte displayNameLen = 18;

        //unicode
        private byte sex_begin;
        private byte sexLen = 2;

        //unicode
        private byte nation_begin;
        private byte nationLen = 10;

        //unicode
        private byte idCardType_begin;
        private byte idCardTypeLen = 10;

        //ascii
        private byte idCardNo_begin;
        private byte idCardNoLen = 20;

        //unicode
        //private byte idCardGov_begin;
        //private byte idCardGovLen = 30;

        //unicode
        private byte addr_begin;
        private byte addrLen = 40;

        //ascii
        private byte phone_begin;
        private byte phoneLen = 12;

        //ascii
        private byte mobile_begin;
        private byte mobileLen = 18;

        //ascii
        private byte cpuCardNo_begin;
        private byte cpuCardNoLen = 20;

        //unicode
        private byte cpuCardType_begin;
        private byte cpuCardTypeLen = 10;

        //ascii
        private byte cpuCardVersion_begin;
        private byte cpuCardVersionLen = 5;

        //unicode
        private byte cpuCardStatus_begin;
        private byte cpuCardStatusLen = 10;

        //ascii 20131110
        //private byte cpuCardOpenDate_begin;
        //private byte cpuCardOpenDateLen = 8;

        //ascii 有效天数比如10天
        //private byte cpuCardValideDays_begin;
        //private byte cpuCardValideDaysLen = 7;

        //ascii 1.2
        private byte fee_begin;
        private byte feeLen = 5;

        #endregion


        /// <summary>
        /// 将用户信息封装为写入cpu卡里面的信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="byteLen">写入cpu卡的信息的字节数</param>
        /// <returns></returns>
        public byte[] TransAccount(CpuAccount account, int byteLen)
        {
            byte[] result = new byte[byteLen];
            StringBuilder sb = new StringBuilder();
            try
            {
                if (account != null && byteLen > 149)
                {
                    byte[] dn = GetUnicodeBytes(account.DisplayName, displayNameLen);
                    FillBytes(result, dn, displayName_begin, displayNameLen);

                    byte[] sex = GetUnicodeBytes(account.Sex, sexLen);
                    FillBytes(result, sex, sex_begin, sexLen);

                    byte[] nation = GetUnicodeBytes(account.Nation, nationLen);
                    FillBytes(result, nation, nation_begin, nationLen);

                    byte[] lxdh = GetAsciiBytes(account.Mobile, mobileLen);
                    FillBytes(result, lxdh, mobile_begin, mobileLen);

                    byte[] phone = GetAsciiBytes(account.Phone, phoneLen);
                    FillBytes(result, phone, phone_begin, phoneLen);

                    byte[] cpuno = GetAsciiBytes(account.CpuCardNo, cpuCardNoLen);
                    FillBytes(result, cpuno, cpuCardNo_begin, cpuCardNoLen);

                    byte[] cputype = GetUnicodeBytes(account.CpuCardType, cpuCardTypeLen);
                    FillBytes(result, cputype, cpuCardType_begin, cpuCardTypeLen);

                    byte[] cpuversion = GetAsciiBytes(account.CpuCardVersion, cpuCardVersionLen);
                    FillBytes(result, cpuversion, cpuCardVersion_begin, cpuCardVersionLen);

                    byte[] cpustatus = GetUnicodeBytes(account.CpuCardStatus, cpuCardStatusLen);
                    FillBytes(result, cpustatus, cpuCardStatus_begin, cpuCardStatusLen);

                    byte[] addr = GetUnicodeBytes(account.Address, addrLen);
                    FillBytes(result, addr, addr_begin, addrLen);

                    byte[] fee = GetAsciiBytes(account.Fee, feeLen);
                    FillBytes(result, fee, fee_begin, feeLen);

                    byte[] ct = GetUnicodeBytes(account.IdCardType, idCardTypeLen);
                    FillBytes(result, ct, idCardType_begin, idCardTypeLen);

                    byte[] cn = GetAsciiBytes(account.IdCardNo, idCardNoLen);
                    FillBytes(result, cn, idCardNo_begin, idCardNoLen);

                }
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 从cpu卡中读取的字节封装为账户对象
        /// </summary>
        /// <param name="fileDatas"></param>
        /// <returns></returns>
        public CpuAccount ReTransAccount(byte[] fileDatas)
        {
            CpuAccount account = new CpuAccount();
            try
            {
                if (fileDatas != null && fileDatas.Count() > 149)
                {
                    byte[] dn = new byte[displayNameLen];
                    dn = ReFillBytes(fileDatas, displayName_begin, displayNameLen);
                    account.DisplayName = Encoding.GetEncoding("gb2312").GetString(dn);

                    byte[] sex = new byte[sexLen];
                    sex = ReFillBytes(fileDatas, sex_begin, sexLen);
                    account.Sex = Encoding.GetEncoding("gb2312").GetString(sex);

                    byte[] nation = new byte[nationLen];
                    nation = ReFillBytes(fileDatas, nation_begin, nationLen);
                    account.Nation = Encoding.GetEncoding("gb2312").GetString(nation);

                    byte[] lxdh = new byte[mobileLen];
                    lxdh = ReFillBytes(fileDatas, mobile_begin, mobileLen);
                    account.Mobile = Encoding.ASCII.GetString(lxdh);

                    byte[] phone = new byte[phoneLen];
                    phone = ReFillBytes(fileDatas, phone_begin, phoneLen);
                    account.Phone = Encoding.ASCII.GetString(phone);

                    byte[] cpuno = new byte[cpuCardNoLen];
                    cpuno = ReFillBytes(fileDatas, cpuCardNo_begin, cpuCardNoLen);
                    account.CpuCardNo = Encoding.ASCII.GetString(cpuno);

                    byte[] cputype = new byte[cpuCardTypeLen];
                    cputype = ReFillBytes(fileDatas, cpuCardType_begin, cpuCardTypeLen);
                    account.CpuCardType = Encoding.GetEncoding("gb2312").GetString(cputype);

                    //以下代码为JSH所添加，目的是获得字节数组的版本号，便于判别
                    account.baCardVer = new byte[cpuCardVersionLen];
                    account.baCardVer = ReFillBytes(fileDatas, cpuCardVersion_begin, cpuCardVersionLen);
                    //以下代码为xzy所写，为保险起见，暂不动
                    byte[] cpuversion = new byte[cpuCardVersionLen];
                    cpuversion = ReFillBytes(fileDatas, cpuCardVersion_begin, cpuCardVersionLen);
                    account.CpuCardVersion = Encoding.ASCII.GetString(cpuversion);

                    byte[] cpustatus = new byte[cpuCardStatusLen];
                    cpustatus = ReFillBytes(fileDatas, cpuCardStatus_begin, cpuCardStatusLen);
                    account.CpuCardStatus = Encoding.GetEncoding("gb2312").GetString(cpustatus);

                    byte[] addr = new byte[addrLen];
                    addr = ReFillBytes(fileDatas, addr_begin, addrLen);
                    account.Address = Encoding.GetEncoding("gb2312").GetString(addr);

                    byte[] fee = new byte[feeLen];
                    fee = ReFillBytes(fileDatas, fee_begin, feeLen);
                    account.Fee = Encoding.ASCII.GetString(fee);

                    byte[] ct = new byte[idCardTypeLen];
                    ct = ReFillBytes(fileDatas, idCardType_begin, idCardTypeLen);
                    account.IdCardType = Encoding.GetEncoding("gb2312").GetString(ct);

                    byte[] cn = new byte[idCardNoLen];
                    cn = ReFillBytes(fileDatas, idCardNo_begin, idCardNoLen);
                    account.IdCardNo = Encoding.ASCII.GetString(cn);

                }
            }
            catch (Exception)
            {
                //提示错误
            }
            return account;
        }

        /// <summary>
        /// 写用户信息到cpu卡里面
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public ResultInfo<byte[]> WriteAccountInfo(CpuAccount account)
        {
            ResultInfo<byte[]> result = new ResultInfo<byte[]>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取读写的权限
                    status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    //ZQ58_Beep(icdev, _beepTime);
                    if (status == 0)
                    {
                        byte addFile = 0x00;
                        byte fileLen = 0xC0;
                        byte[] fileData = new byte[192];
                        fileData = TransAccount(account, 192);
                        status = ZQ58_Write0009(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                        ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                        if (status == 0)
                        {
                            result.ResultNumber = ResultEnum.Success;
                            result.Describe = "成功";
                            result.Obj = fileData;
                            //ZQ58_Beep(icdev, _beepTime);
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        /// <summary>
        /// 获取当前cpu卡用户信息
        /// </summary>
        /// <returns></returns>
        public ResultInfo<CpuAccount> ReadAccountInfo()
        {
            ResultInfo<CpuAccount> result = new ResultInfo<CpuAccount>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取读的权限
                    status = ZQ58_ReadAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    //ZQ58_Beep(icdev, _beepTime);
                    if (status == 0)
                    {
                        byte addFile = 0x00;
                        byte fileLen = 0xC0;
                        byte[] fileData = new byte[192];
                        status = ZQ58_Read0009(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                        ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                        if (status == 0)
                        {
                            CpuAccount account = new CpuAccount();
                            try
                            {
                                if (fileData.Count() > 0)
                                {
                                    account = ReTransAccount(fileData);
                                    result.ResultNumber = ResultEnum.Success;
                                    result.Describe = "成功";
                                    account.CpuCardId = string.Format("{0}{1}{2}{3}", Convert.ToString(cpuid[0], 16).PadLeft(2, '0'), Convert.ToString(cpuid[1], 16).PadLeft(2, '0'), Convert.ToString(cpuid[2], 16).PadLeft(2, '0'), Convert.ToString(cpuid[3], 16).PadLeft(2, '0'));//System.Text.Encoding.ASCII.GetString(cpuid);//把cpuid放进去，cpuid是不存的
                                    account.CpuCardId = string.IsNullOrEmpty(account.CpuCardId)
                                                            ? ""
                                                            : account.CpuCardId.ToUpper();
                                    result.Obj = account;
                                    //ZQ58_Beep(icdev, _beepTime);
                                }
                                else
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    result.Describe = "读取用户信息转换异常";
                                }
                            }
                            catch (Exception e1)
                            {
                                result.ResultNumber = ResultEnum.Error;
                                result.Describe = e1.Message;
                            }
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "\r\n" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "\r\n" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "\r\n" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "\r\n" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        #endregion

        #region finance

        #region 消费记录的信息位置

        private byte dealId_begin;
        private byte dealIdLen = 2;

        private byte creditMoney_begin;
        private byte creditMoneyLen = 3;

        private byte dealMoney_begin;
        private byte dealMoneyLen = 4;

        private byte dealType_begin;
        private byte dealTypeLen = 1;

        private byte terminalId_begin;
        private byte terminalIdLen = 6;

        private byte dealTime_begin;
        private byte dealTimeLen = 7;


        #endregion

        /// <summary>
        /// 将从cpu卡中读出来的串翻译成记录
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        private DealRecord TransConsumeAccord(byte[] datas)
        {
            DealRecord result = new DealRecord();
            try
            {
                if (datas != null && datas.Length == 23)
                {
                    byte[] dealid = new byte[dealIdLen];
                    dealid = ReFillBytes(datas, dealId_begin, dealIdLen);
                    result.DealId = Convert.ToString(dealid[0], 16).PadLeft(2, '0') + Convert.ToString(dealid[1], 16).PadLeft(2, '0');
                    byte[] creadit = new byte[creditMoneyLen];
                    creadit = ReFillBytes(datas, creditMoney_begin, creditMoneyLen);
                    result.CreditMoney = GetOutMoney(BytesToMoney(creadit), 100);
                    byte[] sm = new byte[dealMoneyLen];
                    sm = ReFillBytes(datas, dealMoney_begin, dealMoneyLen);
                    result.DealMoney = GetOutMoney(BytesToMoney(sm), 100);
                    byte dealType = new byte();
                    dealType = ReFillBytes(datas, dealType_begin, dealTypeLen).FirstOrDefault();
                    result.DealType = dealType;
                    byte[] terminalId = new byte[terminalIdLen];
                    terminalId = ReFillBytes(datas, terminalId_begin, terminalIdLen);
                    result.TerminalId = string.Format("{0}{1}{2}{3}{4}{5}", Convert.ToString(terminalId[0], 16).PadLeft(2, '0'), Convert.ToString(terminalId[1], 16).PadLeft(2, '0'), Convert.ToString(terminalId[2], 16).PadLeft(2, '0'), Convert.ToString(terminalId[3], 16).PadLeft(2, '0'), Convert.ToString(terminalId[4], 16).PadLeft(2, '0'), Convert.ToString(terminalId[5], 16).PadLeft(2, '0'));
                    byte[] st = new byte[dealTimeLen];
                    st = ReFillBytes(datas, dealTime_begin, dealTimeLen);
                    result.DealTime = BytesToDateTime(st);
                }
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 钱包充值,钱不存小数点，但是精确到一角钱，所以比方1.1元存为11毛.
        /// 交易金额和日期都是十六进制
        /// 交易金额四字节存的是千、百、十、元？（交易金额可以直接使用无符号32位整数，这样是占四个字节，最大是4294967295）
        /// 交易日期七字节分别存什么？年月日？(交易日期可以分为年的第一位，年的后三位，月，日，时，分，秒，比如2013-1-10 10:10:10 分为2,013,1,10,10,10,10，都用分别用一个字节去存)
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public ResultInfo<byte[]> PurseSave(Decimal money)
        {
            ResultInfo<byte[]> result = new ResultInfo<byte[]>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取写的权限
                    status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    //ZQ58_Beep(icdev, _beepTime);
                    if (status == 0)
                    {
                        //psam卡权限验证
                        status = ZQ58_PsamPin(_cpuEquipmentInfo.Icdev, _psamId, ref _psamPassword[0]);
                        ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                        if (status == 0)
                        {
                            byte[] rData = new byte[28];//返回数据
                            byte[] amount = new byte[4];//充值金额
                            amount = MoneyToBytes(PutInMoney(money, 100), 4);
                            byte[] datetime = new byte[7];//交易时间
                            DateTime now = System.DateTime.Now;
                            datetime =
                                DateTimeToBytes(now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') +
                                            now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') +
                                            now.Second.ToString().PadLeft(2, '0'));
                            status = ZQ58_EPCZ(_cpuEquipmentInfo.Icdev, _psamId, ref amount[0], ref datetime[0], ref rData[0]);
                            if (status == 0)
                            {
                                result.ResultNumber = ResultEnum.Success;
                                result.Describe = "成功";
                                result.Obj = rData;
                                //ZQ58_Beep(icdev, _beepTime);
                            }
                            else
                            {
                                byte[] rLenbs = new byte[1];
                                byte[] rDatabs = new byte[30];
                                status = ZQ58_BSCard(_cpuEquipmentInfo.Icdev, _psamId, ref rLenbs[0], ref rDatabs[0]);
                                if (status == 0)
                                {
                                    result.ResultNumber = ResultEnum.Success;
                                    result.Describe = "成功";
                                    result.Obj = rData;
                                }
                                else
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    byte[] swError = new byte[2];
                                    ZQ58_GetSW(ref swError[0]);
                                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                                }
                            }
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        /// <summary>
        /// 查询钱包余额
        /// </summary>
        /// <returns></returns>
        public ResultInfo<Decimal> QueryPurse()
        {
            ResultInfo<Decimal> result = new ResultInfo<Decimal>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取写的权限
                    status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                    if (status == 0)
                    {
                        byte[] amount = new byte[4];//钱包金额
                        status = ZQ58_EPBalance(_cpuEquipmentInfo.Icdev, _psamId, ref amount[0]);
                        if (status == 0)
                        {
                            result.ResultNumber = ResultEnum.Success;
                            result.Describe = "成功";
                            result.Obj = GetOutMoney(BytesToMoney(amount), 100);
                            //ZQ58_Beep(icdev, _beepTime);
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        public ResultInfo<byte[]> CostMoney(Decimal money)
        {
            ResultInfo<byte[]> result = new ResultInfo<byte[]>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取写的权限
                    status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    //ZQ58_Beep(icdev, _beepTime);
                    if (status == 0)
                    {
                        //psam卡权限验证
                        status = ZQ58_PsamPin(_cpuEquipmentInfo.Icdev, _psamId, ref _psamPassword[0]);
                        ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                        if (status == 0)
                        {
                            byte[] rData = new byte[28];//返回数据
                            byte[] amount = new byte[4];//充值金额
                            amount = MoneyToBytes(PutInMoney(money, 100), 4);
                            byte[] datetime = new byte[7];//交易时间
                            DateTime now = System.DateTime.Now;
                            datetime =
                                DateTimeToBytes(now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') +
                                            now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') +
                                            now.Second.ToString().PadLeft(2, '0'));
                            status = ZQ58_EPXF(_cpuEquipmentInfo.Icdev, _psamId, ref amount[0], ref datetime[0], ref rData[0]);
                            if (status == 0)
                            {
                                result.ResultNumber = ResultEnum.Success;
                                result.Describe = "成功";
                                result.Obj = rData;
                                //ZQ58_Beep(icdev, _beepTime);
                            }
                            else
                            {
                                byte[] rLenbs = new byte[1];
                                byte[] rDatabs = new byte[30];
                                status = ZQ58_BSCard(_cpuEquipmentInfo.Icdev, _psamId, ref rLenbs[0], ref rDatabs[0]);
                                if (status == 0)
                                {
                                    result.ResultNumber = ResultEnum.Success;
                                    result.Describe = "成功";
                                    result.Obj = rData;
                                    //ZQ58_Beep(icdev, _beepTime);
                                }
                                else
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    byte[] swError = new byte[2];
                                    ZQ58_GetSW(ref swError[0]);
                                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                                }
                            }
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        /// <summary>
        /// 根据序号查交易记录，1表示最近一笔，最多只能查到20
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public ResultInfo<DealRecord> GetDealRecords(int num)
        {
            ResultInfo<DealRecord> result = new ResultInfo<DealRecord>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取写的权限
                    status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                    if (status == 0)
                    {
                        if (num < 1 || num > 20)
                            num = 1;
                        byte RecordNo = BitConverter.GetBytes(num)[0];
                        byte[] rData = new byte[23];
                        status = ZQ58_ReadJYRecord(_cpuEquipmentInfo.Icdev, _psamId, RecordNo, ref rData[0]);
                        if (status == 0)
                        {
                            result.ResultNumber = ResultEnum.Success;
                            result.Describe = "成功";
                            result.Obj = TransConsumeAccord(rData);
                            result.Obj.CpuId = string.Format("{0}{1}{2}{3}", Convert.ToString(cpuid[0], 16).PadLeft(2, '0'), Convert.ToString(cpuid[1], 16).PadLeft(2, '0'), Convert.ToString(cpuid[2], 16).PadLeft(2, '0'), Convert.ToString(cpuid[3], 16).PadLeft(2, '0'));
                            result.Obj.CpuId = string.IsNullOrEmpty(result.Obj.CpuId)
                                                   ? ""
                                                   : result.Obj.CpuId.ToUpper();
                            //ZQ58_Beep(icdev, _beepTime);
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        #endregion

        #region bike

        #region 0010文件存的租车信息

        private byte openAccountDate_begin;
        private byte openAccountDateLen = 4;

        private byte endAccountDate_begin;
        private byte endAccountLen = 4;

        private byte borrowBikepsamid_begin;
        private byte borrowBikepsamidLen = 6;

        private byte borrowCpuid_begin;
        private byte borrowCpuidLen = 4;

        private byte borrowBikeid_begin;
        private byte borrowBikeidLen = 4;

        private byte borrowTime_begin;
        private byte borrowTimeLen = 7;

        #endregion

        public byte[] TransBorrowBike(BorrowBike borrowBike, int len)
        {
            byte[] result = new byte[len];
            try
            {
                if (borrowBike != null && len > 0)
                {
                    byte[] openaccount = DateToBytes(borrowBike.OpenAccountDate);
                    FillBytes(result, openaccount, openAccountDate_begin, openAccountDateLen);
                    byte[] endaccount = DateToBytes(borrowBike.EndAccountDate);
                    FillBytes(result, endaccount, endAccountDate_begin, endAccountLen);
                    //byte[] borrowpsamid = GetAsciiBytes(borrowBike.BorrowBikePsamid, borrowBikeidLen);
                    byte[] borrowpsamid = new byte[borrowBikepsamidLen];
                    try
                    {
                        borrowpsamid[0] = Convert.ToByte(borrowBike.BorrowBikePsamid.Substring(0, 2), 16);
                        borrowpsamid[1] = Convert.ToByte(borrowBike.BorrowBikePsamid.Substring(2, 2), 16);
                        borrowpsamid[2] = Convert.ToByte(borrowBike.BorrowBikePsamid.Substring(4, 2), 16);
                        borrowpsamid[3] = Convert.ToByte(borrowBike.BorrowBikePsamid.Substring(6, 2), 16);
                        borrowpsamid[4] = Convert.ToByte(borrowBike.BorrowBikePsamid.Substring(8, 2), 16);
                        borrowpsamid[5] = Convert.ToByte(borrowBike.BorrowBikePsamid.Substring(10, 2), 16);
                    }
                    catch (Exception)
                    {

                    }
                    FillBytes(result, borrowpsamid, borrowBikepsamid_begin, borrowBikepsamidLen);
                    //byte[] borrowcpuid = GetAsciiBytes(borrowBike.BorrowCpuid, borrowCpuidLen);
                    byte[] borrowcpuid = new byte[borrowCpuidLen];
                    try
                    {
                        borrowcpuid[0] = Convert.ToByte(borrowBike.BorrowCpuid.Substring(0, 2), 16);
                        borrowcpuid[1] = Convert.ToByte(borrowBike.BorrowCpuid.Substring(2, 2), 16);
                        borrowcpuid[2] = Convert.ToByte(borrowBike.BorrowCpuid.Substring(4, 2), 16);
                        borrowcpuid[3] = Convert.ToByte(borrowBike.BorrowCpuid.Substring(6, 2), 16);
                    }
                    catch (Exception)
                    {

                    }
                    FillBytes(result, borrowcpuid, borrowCpuid_begin, borrowCpuidLen);
                    //byte[] borrowbikeid = GetAsciiBytes(borrowBike.BorrowBikeid, borrowBikeidLen);
                    byte[] borrowbikeid = new byte[borrowBikeidLen];
                    try
                    {
                        borrowbikeid[0] = Convert.ToByte(borrowBike.BorrowBikeid.Substring(0, 2), 16);
                        borrowbikeid[1] = Convert.ToByte(borrowBike.BorrowBikeid.Substring(2, 2), 16);
                        borrowbikeid[2] = Convert.ToByte(borrowBike.BorrowBikeid.Substring(4, 2), 16);
                        borrowbikeid[3] = Convert.ToByte(borrowBike.BorrowBikeid.Substring(6, 2), 16);
                    }
                    catch (Exception)
                    {

                    }
                    FillBytes(result, borrowbikeid, borrowBikeid_begin, borrowBikeidLen);
                    byte[] borrowtime = DateTimeToBytes(borrowBike.BorrowTime);
                    FillBytes(result, borrowtime, borrowTime_begin, borrowTimeLen);
                }
            }
            catch (Exception)
            {

            }
            return result;
        }

        public BorrowBike ReTransBorrowBike(byte[] datas)
        {
            BorrowBike result = new BorrowBike();
            try
            {
                if (datas != null && datas.Length > 0)
                {
                    byte[] openaccount = ReFillBytes(datas, openAccountDate_begin, openAccountDateLen);
                    result.OpenAccountDate = BytesToDate(openaccount);
                    byte[] endaccount = ReFillBytes(datas, endAccountDate_begin, endAccountLen);
                    result.EndAccountDate = BytesToDate(endaccount);
                    byte[] borrowpsamid = ReFillBytes(datas, borrowBikepsamid_begin, borrowBikepsamidLen);
                    result.BorrowBikePsamid = string.Format("{0}{1}{2}{3}{4}{5}", Convert.ToString(borrowpsamid[0], 16).PadLeft(2, '0'), Convert.ToString(borrowpsamid[1], 16).PadLeft(2, '0'), Convert.ToString(borrowpsamid[2], 16).PadLeft(2, '0'), Convert.ToString(borrowpsamid[3], 16).PadLeft(2, '0'), Convert.ToString(borrowpsamid[4], 16).PadLeft(2, '0'), Convert.ToString(borrowpsamid[5], 16).PadLeft(2, '0'));//Encoding.ASCII.GetString(borrowpsamid).Replace("\0", "");
                    result.BorrowBikePsamid = string.IsNullOrEmpty(result.BorrowBikePsamid)
                                                  ? ""
                                                  : result.BorrowBikePsamid.ToUpper();
                    byte[] borrowcpuid = ReFillBytes(datas, borrowCpuid_begin, borrowCpuidLen);
                    result.BorrowCpuid = string.Format("{0}{1}{2}{3}", Convert.ToString(borrowcpuid[0], 16).PadLeft(2, '0'), Convert.ToString(borrowcpuid[1], 16).PadLeft(2, '0'), Convert.ToString(borrowcpuid[2], 16).PadLeft(2, '0'), Convert.ToString(borrowcpuid[3], 16).PadLeft(2, '0'));//Encoding.ASCII.GetString(borrowcpuid).Replace("\0", "");
                    result.BorrowCpuid = string.IsNullOrEmpty(result.BorrowCpuid) ? "" : result.BorrowCpuid.ToUpper();
                    byte[] borrowbikeid = ReFillBytes(datas, borrowBikeid_begin, borrowBikeidLen);
                    result.BorrowBikeid = string.Format("{0}{1}{2}{3}", Convert.ToString(borrowbikeid[0], 16).PadLeft(2, '0'), Convert.ToString(borrowbikeid[1], 16).PadLeft(2, '0'), Convert.ToString(borrowbikeid[2], 16).PadLeft(2, '0'), Convert.ToString(borrowbikeid[3], 16).PadLeft(2, '0'));//Encoding.ASCII.GetString(borrowbikeid).Replace("\0", "");
                    result.BorrowBikeid = string.IsNullOrEmpty(result.BorrowBikeid) ? "" : result.BorrowBikeid.ToUpper();
                    byte[] borrowtime = ReFillBytes(datas, borrowTime_begin, borrowTimeLen);
                    result.BorrowTime = BytesToDateTime(borrowtime);
                }
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 写借车信息到cpu卡里面
        /// </summary>
        /// <param name="borrowBike"></param>
        /// <returns></returns>
        public ResultInfo<byte[]> WriteBorrowBikeInfo(BorrowBike borrowBike)
        {
            ResultInfo<byte[]> result = new ResultInfo<byte[]>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取读写的权限
                    status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                    if (status == 0)
                    {
                        byte addFile = 0x00;
                        byte fileLen = 0xC0;
                        byte[] fileData = new byte[192];
                        fileData = TransBorrowBike(borrowBike, 192);
                        status = ZQ58_Write0010(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                        //ZQ58_Beep(icdev, _beepTime);
                        if (status == 0)
                        {
                            result.ResultNumber = ResultEnum.Success;
                            result.Describe = "成功";
                            result.Obj = fileData;
                            //ZQ58_Beep(icdev, _beepTime);
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        /// <summary>
        /// 获取当前cpu卡用户借车信息
        /// </summary>
        /// <returns></returns>
        public ResultInfo<BorrowBike> ReadBorrowBikeInfo()
        {
            ResultInfo<BorrowBike> result = new ResultInfo<BorrowBike>();
            int status = -1;
            if (!_cpuEquipmentInfo.Status)
            {
                CheckCpuEquipment();
            }
            if (_cpuEquipmentInfo.Status)
            {
                byte[] cpuid = new byte[6];//cpu卡序列号
                byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                if (status == 0)
                {
                    //获取读的权限
                    status = ZQ58_ReadAuth(_cpuEquipmentInfo.Icdev, _psamId);
                    ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                    if (status == 0)
                    {
                        byte addFile = 0x00;
                        byte fileLen = 0xC0;
                        byte[] fileData = new byte[192];
                        status = ZQ58_Read0010(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                        //ZQ58_Beep(icdev, _beepTime);
                        if (status == 0)
                        {
                            BorrowBike temp = new BorrowBike();
                            try
                            {
                                if (fileData.Count() > 0)
                                {
                                    temp = ReTransBorrowBike(fileData);
                                    result.ResultNumber = ResultEnum.Success;
                                    result.Describe = "成功";
                                    result.Obj = temp;
                                    //ZQ58_Beep(icdev, _beepTime);
                                }
                                else
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    result.Describe = "读取用户信息转换异常";
                                }
                            }
                            catch (Exception e1)
                            {
                                result.ResultNumber = ResultEnum.Error;
                                result.Describe = e1.Message;
                            }
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                byte[] swError = new byte[2];
                ZQ58_GetSW(ref swError[0]);
                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
            }
            return result;
        }

        #endregion

        #region 独立完成功能的方法比方说开户，一个方法完成所有开户关于cpu卡的流程

        /// <summary>
        /// 用户卡开户
        /// </summary>
        /// <param name="account"></param>
        /// <param name="money"></param>
        /// <returns>返回卡余额</returns>
        public ResultInfo<decimal> OpenAccount(CpuAccount account, decimal money)
        {
            ResultInfo<decimal> result = new ResultInfo<decimal>() { ResultNumber = ResultEnum.Error, Obj = 0 };
            try
            {
                int status = -1;
                if (!_cpuEquipmentInfo.Status)
                {
                    CheckCpuEquipment();
                }
                if (_cpuEquipmentInfo.Status)
                {
                    byte[] cpuid = new byte[6];//cpu卡序列号
                    byte[] lfen = new byte[1];//CPU卡的0014长文件首字节长度
                    status = ZQ58_CpuInit(_cpuEquipmentInfo.Icdev, _psamId, ref cpuid[0], ref lfen[0]);
                    if (status == 0)
                    {
                        //获取读写的权限
                        status = ZQ58_WriteAuth(_cpuEquipmentInfo.Icdev, _psamId);
                        //ZQ58_Beep(icdev, _beepTime);
                        if (status == 0)
                        {
                            byte addFile = 0x00;
                            byte fileLen = 0xC0;
                            byte[] fileData = new byte[192];

                            //读用户卡
                            //验证卡片有效性
                            status = ZQ58_Read0009(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                            if (status == 0)
                            {
                                CpuAccount ac = new CpuAccount();
                                try
                                {
                                    if (fileData.Count() > 0)
                                    {
                                        ac = ReTransAccount(fileData);
                                        if (ac != null && Converts.Replace0Str(ac.CpuCardStatus) == CpuCardStatus.Logout)
                                        {
                                            result.ResultNumber = ResultEnum.Error;
                                            result.Describe = "卡片已注销，需要提交给应用开发商初始化，不能直接重新使用";
                                            return result;
                                        }
                                    }
                                    else
                                    {
                                        result.ResultNumber = ResultEnum.Error;
                                        result.Describe += "读取用户信息转换异常";
                                    }
                                }
                                catch (Exception e1)
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    result.Describe += e1.Message;
                                }
                            }

                            //写用户卡
                            //写用户信息
                            fileData = new byte[192];
                            fileData = TransAccount(account, 192);
                            status = ZQ58_Write0009(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                            if (status == 0)
                            {
                                //写借车信息，这里用作过期功能
                                fileData = new byte[192];
                                fileData = TransBorrowBike(new BorrowBike()
                                {
                                    OpenAccountDate = Converts.ConvertDateToString(DateTime.Now),
                                    EndAccountDate = Converts.ConvertDateToString(DateTime.Now.AddYears(3))
                                }, 192);
                                status = ZQ58_Write0010(_cpuEquipmentInfo.Icdev, _psamId, addFile, fileLen, ref fileData[0]);
                                if (status == 0)
                                {
                                    //psam卡权限验证
                                    status = ZQ58_PsamPin(_cpuEquipmentInfo.Icdev, _psamId, ref _psamPassword[0]);
                                    if (status == 0)
                                    {

                                        //存入初始金额
                                        byte[] rData = new byte[28];//返回数据
                                        byte[] amount = new byte[4];//充值金额
                                        amount = MoneyToBytes(PutInMoney(money, 100), 4);
                                        byte[] datetime = new byte[7];//交易时间
                                        DateTime now = System.DateTime.Now;
                                        datetime =
                                            DateTimeToBytes(now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') +
                                                        now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') +
                                                        now.Second.ToString().PadLeft(2, '0'));
                                        status = ZQ58_EPCZ(_cpuEquipmentInfo.Icdev, _psamId, ref amount[0], ref datetime[0], ref rData[0]);
                                        if (status == 0)
                                        {
                                            //读出当前卡的余额
                                            byte[] amount1 = new byte[4];//钱包金额
                                            status = ZQ58_EPBalance(_cpuEquipmentInfo.Icdev, _psamId, ref amount1[0]);
                                            if (status == 0)
                                            {
                                                result.ResultNumber = ResultEnum.Success;
                                                result.Describe = "成功";
                                                result.Obj = GetOutMoney(BytesToMoney(amount1), 100);
                                                ZQ58_Beep(_cpuEquipmentInfo.Icdev, _beepTime);
                                            }
                                            else
                                            {
                                                result.ResultNumber = ResultEnum.Error;
                                                byte[] swError = new byte[2];
                                                ZQ58_GetSW(ref swError[0]);
                                                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                                            }
                                        }
                                        else
                                        {
                                            result.ResultNumber = ResultEnum.Error;
                                            byte[] swError = new byte[2];
                                            ZQ58_GetSW(ref swError[0]);
                                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                                        }
                                    }
                                    else
                                    {
                                        result.ResultNumber = ResultEnum.Error;
                                        byte[] swError = new byte[2];
                                        ZQ58_GetSW(ref swError[0]);
                                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                                    }
                                }
                                else
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    byte[] swError = new byte[2];
                                    ZQ58_GetSW(ref swError[0]);
                                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                                }
                            }
                            else
                            {
                                result.ResultNumber = ResultEnum.Error;
                                byte[] swError = new byte[2];
                                ZQ58_GetSW(ref swError[0]);
                                result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                            }
                        }
                        else
                        {
                            result.ResultNumber = ResultEnum.Error;
                            byte[] swError = new byte[2];
                            ZQ58_GetSW(ref swError[0]);
                            result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        byte[] swError = new byte[2];
                        ZQ58_GetSW(ref swError[0]);
                        result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    byte[] swError = new byte[2];
                    ZQ58_GetSW(ref swError[0]);
                    result.Describe = GetZ58ErrorInfo(status) + "//" + GetZ58SWErrorInfo(swError[0], swError[1]);
                }
            }
            catch (Exception e)
            {
                result.Describe += e.Message;
            }
            return result;
        }

        #endregion

        #region 备份方法
        /// <summary>
        /// 钱转为bytes
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        private byte[] MoneyToBytesx(Int32 money)
        {
            byte[] result = new byte[4];
            if (money > 0)
            {
                try
                {
                    string ms = money.ToString();
                    if (ms.Length > 8)
                    {
                        ms = ms.Substring(0, 8);
                    }
                    else
                    {
                        ms = ms.PadLeft(8, '0');
                    }
                    result[0] = BitConverter.GetBytes(Convert.ToInt16(ms.Substring(0, 2)))[0];
                    result[1] = BitConverter.GetBytes(Convert.ToInt16(ms.Substring(2, 2)))[0];
                    result[2] = BitConverter.GetBytes(Convert.ToInt16(ms.Substring(4, 2)))[0];
                    result[3] = BitConverter.GetBytes(Convert.ToInt16(ms.Substring(6, 2)))[0];
                }
                catch (Exception)
                {
                    result = new byte[4];
                }
            }
            return result;
        }

        /// <summary>
        /// bytes转为钱
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private Int32 BytesToMoneyx(byte[] bytes)
        {
            Int32 result = 0;
            if (bytes != null && bytes.Length == 4)
            {
                try
                {
                    string ms = string.Format("{0}{1}{2}{3}", bytes[0].ToString().PadLeft(2, '0'), bytes[1].ToString().PadLeft(2, '0'), bytes[2].ToString().PadLeft(2, '0'), bytes[3].ToString().PadLeft(2, '0'));
                    if (!string.IsNullOrEmpty(ms))
                    {
                        result = Convert.ToInt32(ms);
                    }
                }
                catch (Exception)
                {
                    result = 0;
                }
            }
            return result;
        }

        /// <summary>
        /// 将时间20131111101010转为7位byte型，年2位，月。。。分别一位共七位
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private byte[] DateToBytesx(string date)
        {
            byte[] result = new byte[7];
            if (!string.IsNullOrEmpty(date))
            {
                if (date.Length > 14)
                {
                    date = date.Substring(0, 14);
                }
                else
                {
                    date = date.PadRight(14, '0');
                }
                try
                {
                    byte[] year1 = BitConverter.GetBytes(Convert.ToInt16(date.Substring(0, 2)));
                    byte[] year2 = BitConverter.GetBytes(Convert.ToInt16(date.Substring(2, 2)));
                    byte[] month = BitConverter.GetBytes(Convert.ToInt16(date.Substring(4, 2)));
                    byte[] day = BitConverter.GetBytes(Convert.ToInt16(date.Substring(6, 2)));
                    byte[] hour = BitConverter.GetBytes(Convert.ToInt16(date.Substring(8, 2)));
                    byte[] min = BitConverter.GetBytes(Convert.ToInt16(date.Substring(10, 2)));
                    byte[] second = BitConverter.GetBytes(Convert.ToInt16(date.Substring(12, 2)));
                    result[0] = year1[0];
                    result[1] = year2[0];
                    result[2] = month[0];
                    result[3] = day[0];
                    result[4] = hour[0];
                    result[5] = min[0];
                    result[6] = second[0];
                }
                catch (Exception e)
                {
                    result = new byte[7];
                }
            }
            return result;
        }

        /// <summary>
        /// 时间的bytes转换为string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private string BytesToDatex(byte[] bytes)
        {
            string result = string.Empty;
            if (bytes != null && bytes.Length == 7)
            {
                try
                {
                    byte[] y1 = new byte[2];
                    y1[0] = bytes[0];
                    byte[] y2 = new byte[2];
                    y2[0] = bytes[1];
                    int year1 = BitConverter.ToInt16(y1, 0);
                    int year2 = BitConverter.ToInt16(y2, 0);
                    byte[] m = new byte[2];
                    m[0] = bytes[2];
                    int month = BitConverter.ToInt16(m, 0);
                    byte[] d = new byte[2];
                    d[0] = bytes[3];
                    int day = BitConverter.ToInt16(d, 0);
                    byte[] h = new byte[2];
                    h[0] = bytes[4];
                    int hour = BitConverter.ToInt16(h, 0);
                    byte[] mi = new byte[2];
                    mi[0] = bytes[5];
                    int min = BitConverter.ToInt16(mi, 0);
                    byte[] se = new byte[2];
                    se[0] = bytes[6];
                    int second = BitConverter.ToInt16(se, 0);
                    result = string.Format("{0}{1}{2}{3}{4}{5}{6}", year1.ToString().PadLeft(2, '0'), year2.ToString().PadLeft(2, '0'), month.ToString().PadLeft(2, '0'), day.ToString().PadLeft(2, '0'), hour.ToString().PadLeft(2, '0'), min.ToString().PadLeft(2, '0'), second.ToString().PadLeft(2, '0'));
                }
                catch (Exception)
                {
                    result = string.Empty;
                }
            }
            return result;
        }
        #endregion

    }
}
